from PyQt4.QtGui import QWidget, QLabel, QPixmap
from ..widgets.mixed_button import MixedButton
from ..widgets.image_button import ImageButton
from . import screen_manager
import random

#
# let's have some tooltip text
#
s = ""
s += "Visiting caravans bring slavegirls as well as other goods.\n"
s += "The best of them are earmarked for private buyers or are quickly\n"
s += "whisked away to high class auction houses in the city proper.\n"
s += "The rest, many in poor health after the long desert trek,\n"
s += "are sold at the Souk Auction Block."
souk_market_tt = s

s = ""
s += "The Souk: an open air market outside the city walls\n"
s += "The market is surrounded by an extensive tent city,\n"
s += "populated by caravaneers, transients and those too poor\n" 
s += "to afford housing behind the protection of city walls."
souk_tt = s


class TownScreen(QWidget):
	def __init__(self, *args, **kw):
		QWidget.__init__(self, *args, **kw)
		self.setGeometry(0, 0, 1024, 768)
#
#		put up a full screen map.
#
		pic = QLabel(self)
		pic.setGeometry(0, 0, 1024, 768)
		path = "resources/city_map.png"
		pic.setPixmap(QPixmap(path))
#
#		there's a patch in the bottom right
#		where we can squeeze in a back button
#
		back_butt = MixedButton("Back", self)
		back_butt.setGeometry(800, 690, 210, 50)
		back_butt.clicked.connect(
			self.to_main_screen
		)
#
#		slave market button
#
		slave_market_button = ImageButton(
			"slave_market", self, 
			"resources/slave_market_icon.png",
			"resources/slave_market_icon.png",
			"resources/slave_market_icon.png",
			"resources/slave_market_icon.png"
		)
		slave_market_button.setGeometry(130, 100, 50, 50)
		slave_market_button.clicked.connect(
			self.to_slave_market
		)
		slave_market_button.setToolTip(souk_market_tt)

#
#		another button for the souk as a location
#		this works out as a "walk around town" button
#		giving access to encounter, plot events, 
#		occasional traders with cheap or rare goods
#		and even a potential whore from time to time
#

		souk_button = ImageButton("souk", self, 
			"resources/souk_icon.png",
			"resources/souk_icon.png",
			"resources/souk_icon.png",
			"resources/souk_icon.png"
		)
		souk_button.setGeometry(120, 620, 50, 50)
		souk_button.clicked.connect(
			self.to_souk
		)
		souk_button.setToolTip(souk_tt)

		element_plaza_button = ImageButton("element_plaza", self, 
			"resources/market_icon.png",
			"resources/market_icon.png",
			"resources/market_icon.png",
			"resources/market_icon.png"
		)
		element_plaza_button.setGeometry(530, 400, 50, 50)
		element_plaza_button.clicked.connect(
			self.to_element_plaza
		)
		s = "Four Elements Plaza: A paved square at the junction of\n"
		s += "the two trade roads. Busy with Souk traders, shopping\n"
		s += "townsfolk, pickpockets and whores"
		element_plaza_button.setToolTip(s)

		earth_gate_button = ImageButton("earth_gate", self, 
			"resources/earth_gate_icon.png",
			"resources/earth_gate_icon.png",
			"resources/earth_gate_icon.png",
			"resources/earth_gate_icon.png"
		)
		earth_gate_button.setGeometry(585, 580, 50, 50)
		earth_gate_button.clicked.connect(
			self.to_element_plaza
		)
		s = "The Earth Gate: Entrance to the Earth Quarter, where\n"
		s += "can be found the city's bankers, traders, guild houses\n"
		s += "merchants, factors and agents."
		earth_gate_button.setToolTip(s)

		bank_button = ImageButton("bank", self, 
			"resources/bank_icon.png",
			"resources/bank_icon.png",
			"resources/bank_icon.png",
			"resources/bank_icon.png"
		)
		bank_button.setGeometry(490, 550, 50, 50)
		bank_button.clicked.connect(
			self.to_bank
		)
		s = "The Exchange Hall. Here the city's bankers and money\n"
		s += "changers gather to do business. Come here to bank your\n"
		s += "money, negotiate a loan, or to seek a business partner"
		bank_button.setToolTip(s)

		fire_gate_button = ImageButton("earth_gate", self, 
			"resources/fire_gate_icon.png",
			"resources/fire_gate_icon.png",
			"resources/fire_gate_icon.png",
			"resources/fire_gate_icon.png"
		)
		fire_gate_button.setGeometry(330, 425, 50, 50)
		fire_gate_button.clicked.connect(
			self.to_fire_gate
		)
		s = "The Gate of Fire: Entrance to the Fire Quarter, which\n"
		s += "the city watch as well as the council chambers.\n"
		s += "The Fire Quarter is a good place to recruit bodyguards\n"
		s += "and mercenaries."
		fire_gate_button.setToolTip(s)

		water_gate_button = ImageButton("earth_gate", self, 
			"resources/water_gate_icon.png",
			"resources/water_gate_icon.png",
			"resources/water_gate_icon.png",
			"resources/water_gate_icon.png"
		)
		water_gate_button.setGeometry(456, 182, 50, 50)
		water_gate_button.clicked.connect(
			self.to_water_gate
		)
		s = "The Gate of Water.\n"
		s += "The Water Quarter is spiritual and artistic heart of\n"
		s += "the Crossing."
		water_gate_button.setToolTip(s)

		air_gate_button = ImageButton("air_gate", self, 
			"resources/air_gate_icon.png",
			"resources/air_gate_icon.png",
			"resources/air_gate_icon.png",
			"resources/air_gate_icon.png"
		)
		air_gate_button.setGeometry(740, 360, 50, 50)
		air_gate_button.clicked.connect(
			self.to_air_gate
		)
		s = "The Gate of the Air.\n"
		s += "The Air Quarter, dedicated to reasoned discourse and\n"
		s += "earnest discovery, is home to the majority of \n"
		s += "the Crossing's inventors, as well as artificers,\n"
		s += "explorers, dramatists and scholars of all kinds."
		air_gate_button.setToolTip(s)

		self.hide()

	#
	# later for these ...
	#
	#souk_encounters = WeightedList(
	#	(30, self.no_encounter),
	#	(10, self.trader_rare),
	#	(10, self.trader_cheap),
	#	(5, self.healer_encounter),
	#	(5, self.npc_encounter),
	#	(5, self.girl_encounter),
	#)

	def to_souk(self):
		rval = random.random()
		chance = 0.0
#
#		30% chance of nothing happening
#
		chance = 0.3
		if rval < chance:
			self.no_encounter()
			return
#
#		
#
		chance += 0.1
		if rval < chance:
			self.no_encounter()
			return
		#screen_manager.show("souk")
		pass
	def to_main_screen(self):
		screen_manager.show("main screen")
	def to_slave_market(self):
		screen_manager.show("slave market screen")
		pass
	def to_earth_gate(self):
		#screen_manager.show("earth quarter")
		pass
	def to_element_plaza(self):
		#screen_manager.show("element plaza")
		pass
	def to_bank(self):
		#screen_manager.show("bank")
		pass
	def to_water_gate(self):
		#screen_manager.show("water quarter")
		pass
	def to_fire_gate(self):
		#screen_manager.show("fire quarter")
		pass
	def to_air_gate(self):
		#screen_manager.show("air quarter")
		pass

#
#	pick up key pressed events
#
#	this belongs at app level, really
#	but this'll do for testing
#
	def keyPressEvent(self,e):
		n = -1
#
#		as the above makes clear, there are far more
#		places than there are sensible key combos
#
		k = e.key()
		if k == ord("M"):
			e.accept()
			screen_manager.show("main screen")
			return
		if k == ord("G"):
			e.accept()
			screen_manager.show("tarts screen")
			return
		if k == ord("S"):
			e.accept()
			screen_manager.show("slave market screen")
			return
		e.ignore()



